<template>
  <div>
    <el-form :inline="true" :model="form" label-width="80px">
      <el-form-item>
        <el-input v-model="tempSearchObj.roleName" placeholder="角色名称" />
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
      <el-button @click="resetSearch">清空</el-button>
    </el-form>
    <div style="margin-bottom:20px">
      <el-button type="primary" @click="addRole">添加</el-button>
      <el-button type="danger" :disabled="multiple" @click="removeRoles">批量删除</el-button>
    </div>
    <el-table
      style="width: 100%"
      :data="rolesData"
      border
      stripe
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
      <!-- <el-table-column type="index" label="序号" width="180" align="center"></el-table-column> -->
      <el-table-column label="角色名称" width="width" show-overflow-tooltip align="center">
        <template slot-scope="{row, $index}">
          <div>
            <template v-if="row.edit">
              <div>
                <el-input v-model="row.roleName" style="padding-right:100px;" size="small" />
                <el-button
                  style="position: absolute;right: 15px;top: 10px;"
                  type="warning"
                  size="small"
                  icon="el-icon-refresh"
                  @click="cancelEdit(row)"
                >取消</el-button>
              </div>
            </template>

            <span v-else>{{ row.roleName }}</span>
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="gmtCreate" label="创建时间" width="width" align="center" />
      <el-table-column prop="gmtModified" label="更新时间" width="width" align="center" />-->
      <el-table-column label="操作" type="index" width="300" align="center">
        <template slot-scope="{row, $index}">
          <HintButton
            title="分配权限"
            type="info"
            icon="el-icon-info"
            size="mini"
            @click="assignPermission(row)"
          />
          <HintButton
            v-if="!row.edit"
            title="修改角色"
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="row.edit= true"
          />
          <HintButton
            v-if="row.edit"
            title="确定"
            type="primary"
            icon="el-icon-check"
            size="mini"
            @click="updateRole(row)"
          />
          <HintButton
            title="删除角色"
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="removeRole(row)"
          />
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
      style="padding: 20px 0;"
      layout="prev, pager, next, jumper, ->, sizes, total"
      @current-change="getRoles"
      @size-change="handleSizeChange"
    />
  </div>
</template>
<script>
// import role from '../../../api/acl/role'
export default {
  name: 'PermissionRole',
  data() {
    return {
      listLoading: true, // 数据是否正在加载
      // 批量删除id数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      rolesData: [], // 角色列表
      total: 0, // 总记录数
      page: 1, // 当前页码
      limit: 5, // 每页记录数
      form: {},
      tempSearchObj: {
        username: ''
      },
      searchObj: {
        username: ''
      }
    }
  },
  mounted() {
    this.getRoles()
  },
  methods: {
    // 分页
    async getRoles() {
      const { page, limit, searchObj } = this
      const result = await this.$API.role1.reqGetPageList(
        page,
        limit,
        searchObj
      )
      // console.log('rte', result)
      if (result.code === 20000) {
        const { items, total } = result.data
        this.rolesData = items.map(item => {
          // console.log('uiui', item)
          // 用于标识是否显示编辑输入框的属性
          item.edit = false
          // 缓存角色名称, 用于取消
          item.originRoleName = item.roleName
          return item
        })
        this.total = total
      }
    },
    // 每页数量发生改变的监听
    handleSizeChange(limit) {
      //
      this.limit = limit
      this.getRoles()
    },
    // 查询
    search() {
      this.searchObj = { ...this.tempSearchObj }
      this.getRoles()
    },
    // 清空
    resetSearch() {
      //
      this.searchObj = {}
      this.tempSearchObj = {}
      this.getRoles()
    },
    // 添加
    addRole() {
      // 显示添加页面
      this.$prompt('请输入新角色名称', '添加角色', {
        confirmButtonText: '确认',
        cancelButtonText: '取消'
      }).then(async(res) => {
        if (res.value === '' || res.value === null || res.value.split(' ').join('').length === 0) {
          this.$message.warning('请填写角色名称')
          // alert('请填写角色名称')
          return
        }
        console.log('vvv', res)
        // 添加角色请求
        await this.$API.role1.reqSave({ roleName: res.value })
        this.$message.success(res.message || '添加角色成功')
        this.getRoles()
      }).catch(() => {
        this.$message.warning('取消添加')
      })
    },
    // 批量删除按钮
    async removeRoles() {
      console.log(123)
      const ids = this.ids
      console.log('ids', ids)
      // return
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await this.$API.role1.reqBatchRemoves(ids)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getRoles()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    //
    handleSelectionChange(selection) {
      console.log('selection', selection)
      this.ids = selection.map(item => item.id)
      // 非单个禁用
      this.single = selection.length !== 1
      // 非多个禁用
      this.multiple = !selection.length
    },
    // input框--取消按钮
    cancelEdit(role) {
      console.log('roro', role)
      role.roleName = role.originRoleName
      role.edit = false
      this.$message.warning('取消角色修改')
    },
    // 分配权限
    assignPermission(row) {
      // 路由跳转
      // console.log(111, row.id, row.roleName)
      this.$router.push({
        // path: '/permission/role/auth/',
        // query: { id: row.id, roleName: row.roleName }
        path: `/permission/role/auth/${row.id}`,
        query: {
          roleName: row.roleName
        }
      })
      // this.$router.push(`/permission/role/auth/${row.id}?roleName=${row.roleName}`)
    },
    // 确定按钮
    async updateRole(role) {
      //
      const { id, roleName } = role
      // console.log('123', id, roleName)
      const result = await this.$API.role1.reqUpdateById({ id, roleName })
      if (result.code === 20000) {
        this.$message.success(result.message || '更新角色成功!')
        this.getRoles(this.page)
      }
    },
    // 删除角色
    async removeRole(role) {
      console.log('klkl', role)
      this.$confirm(`确认删除 '${role.roleName}' 吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await this.$API.role1.reqRemove(role.id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getRoles()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
